//
//  CMDBSliderRightViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/25.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

typealias CMDBResetBlock = (NSArray?) -> ()

typealias CMDBCommitBlock = (NSArray?) -> ()

private let KSideSlipLeading : CGFloat   = 60

private let KAnimationDuration : CGFloat = 0.3

private let KRightWidth : CGFloat        = CMDB_Screen_Width - KSideSlipLeading

private var sponsorKey : String = "msponsorKey"


class CMDBSliderRightViewController: NormalTitleViewController {
    
    weak var sponsor : UIViewController?
    
    var commitBlock : CMDBCommitBlock?
    
    var resetBlock : CMDBResetBlock?
    
    var animationDuration : CGFloat = 0
    
    var sideSlipLeading : CGFloat   = 0
    
    var bottomView : UIView?
    
    var mainTableView : CMDBFilterMainTableView?
    
    var thisWindow : UIWindow!
    
    lazy var backView : UIView = {
        
        let backView = UIView(frame: CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: CMDB_Screen_Height))
        backView.backgroundColor = word54Color
        
        let gester = UITapGestureRecognizer.init(target: self, action: #selector(clickBackCover(_:)))
        
        backView.addGestureRecognizer(gester)
        
        return backView
    }()
    
    init(sponsor : UIViewController,commitBlock : CMDBCommitBlock,resetBlock:CMDBResetBlock){
        
        super.init(nibName: nil, bundle: nil)
    
        self.commitBlock = commitBlock
        
        self.resetBlock  = resetBlock
        
        self.sponsor = sponsor
        
        let nav = CustomNavigationControllerViewController(rootViewController: self,hideNavBar: true)
        
        self.filterNav = nav
        
        self.navigationController?.view.frame      = CGRectMake(CMDB_Screen_Width, 0, KRightWidth, CMDB_Screen_Height)
        
        confitgureStatic()
        
        configureUI()
        
        configureData()
        
        printLog(CMDB_Screen_Height)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension CMDBSliderRightViewController{
    
    override func buildTitleView() {
        super.buildTitleView()
        
        let singeLine = UIView.CreateLine(CGRect(x: 0, y: 63.5, width: KRightWidth, height: 0.5), lineColor: word38Color)
        
        titleView?.addSubview(singeLine)
        
        let button                    = UIButton(frame : CGRect(x: 0, y: 0, width: 100, height: 64))
        button.center                 = CGPoint(x: 20, y: self.titleView!.middleY + 10)
        button.imageView?.contentMode = .Center
        button.setImage(UIImage(named: "灰色返回"),     forState: .Normal)
        button.setImage(UIImage(named: "灰色返回"), forState: .Highlighted)
        
        self.titleView?.addSubview(button)
        
        button.addTarget(self, action: #selector(dismiss), forControlEvents: .TouchUpInside)
        
        
        titleView?.backgroundColor = UIColor.whiteColor()
    }
    
    func show(){
        
        sponsor!.navigationController!.view.addSubview(backView)
        
        sponsor!.navigationController!.addChildViewController(navigationController!)
        
        sponsor!.navigationController!.view.addSubview(navigationController!.view)
        
        backView.hidden = true
        
        UIView.animateWithDuration(0.6, animations: { 
                self.navigationController?.view.frame = CGRectMake(60, 0, KRightWidth, CMDB_Screen_Height)
                self.sponsor!.tabBarController!.tabBar.alpha = 0.0;
            }) { (finished) in
                self.backView.hidden = false
                self.sponsor!.tabBarController!.tabBar.alpha = 0.0;
        }
    }
    
    func dismiss(){
        self.sponsor!.tabBarController!.tabBar.alpha = 0.0;
        UIView.animateWithDuration(0.3, animations: {
                self.sponsor!.tabBarController!.tabBar.hidden = false;
            
                self.navigationController?.view.frame = CGRectMake(CMDB_Screen_Width, 0, KRightWidth, CMDB_Screen_Height)
            
            }) { (finished) in
                
                self.sponsor!.tabBarController!.tabBar.alpha = 1.0;
                
                self.backView.removeFromSuperview()
                
                self.navigationController?.view.removeFromSuperview()
                
                self.navigationController?.removeFromParentViewController()
                
        }
    }
}

//action method
extension CMDBSliderRightViewController {

    @objc func clickBackCover(gester : UITapGestureRecognizer){
        
        dismiss()
        
    }
    
}

extension CMDBSliderRightViewController {
    
    weak var filterNav : UINavigationController? {
        
        get{
            
            return (objc_getAssociatedObject(self.sponsor, &sponsorKey) as? UINavigationController)!
            
        }
        set{
            objc_setAssociatedObject(self.sponsor,&sponsorKey ,newValue , objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
    }
    
}

extension CMDBSliderRightViewController{
    
    func configureUI(){
    
        buildMainView()
        
        buildBottomView()
        
    }
    
    func buildMainView(){
        
        mainTableView = CMDBFilterMainTableView()
        
        mainTableView?.mDelegate = self
        
        mainTableView?.separatorStyle = .None
        
        mainTableView?.frame = CGRect(x: 0, y: 64, width: KRightWidth, height: CMDB_Screen_Height - 64 - 49)
        
        mainTableView?.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(mainTableView!)
        
    }
    
    func buildBottomView(){
        
        bottomView        = UIView.getInstance()
        
        bottomView?.frame = CGRect(x: 0, y: CMDB_Screen_Height - 49, width: KRightWidth, height: 49)
        
        self.navigationController!.view.addSubview(bottomView!)
        
        let resetBtn      = UIButton()
        
        resetBtn.frame    = CGRect(x: 0, y: 0, width: KRightWidth * 0.5, height: 49)
        
        resetBtn.setTitleColor(word87Color, forState: .Normal)
        
        resetBtn.backgroundColor   = UIColor.whiteColor()
        
        resetBtn.setTitle("重置", forState: .Normal)
        
        resetBtn.titleLabel?.font  = CMDB_16_FONT
        
        resetBtn.addTarget(self, action: #selector(resetClick), forControlEvents: .TouchUpInside)
        
        let commitBtn              = UIButton()
        
        commitBtn.backgroundColor  = UIColor.whiteColor()
        
        commitBtn.setTitle("确定", forState: .Normal)
        
        commitBtn.setTitleColor(word87Color, forState: .Normal)
        
        commitBtn.titleLabel?.font = CMDB_16_FONT
        
        commitBtn.frame            = CGRect(x: KRightWidth * 0.5, y: 0, width: KRightWidth * 0.5, height: 49)
        
        commitBtn.addTarget(self, action: #selector(commitClick), forControlEvents: .TouchUpInside)
        
        bottomView?.addSubview(resetBtn)
        
        bottomView?.addSubview(commitBtn)
        
        let horizontalView = UIView.CreateLine(CGRect(x: 0, y: 0, width: KRightWidth, height: 0.5), lineColor: word54Color)
        
        bottomView?.addSubview(horizontalView)
        
        let verticalView   = UIView.CreateLine( CGRect(x: KRightWidth * 0.5, y: 0, width: 0.5, height: 49), lineColor: word54Color)
        
        bottomView?.addSubview(verticalView)
    }

    func confitgureStatic(){
        
        animationDuration = KAnimationDuration
        
        sideSlipLeading   = KSideSlipLeading
        
    }
    
    func configureData(){
        
    
    }
}

//MARK:- method action
extension CMDBSliderRightViewController{
    
    @objc func commitClick(){
        
        printLog("commitClick")
    }
    
    @objc func resetClick(){
        
        printLog("resetClick")
        
    }
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        printLog("titleClick")
    }
    
}


